<?php
namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        echo "111";
    }

    public function shou()
    {
        $type = M('type')->field('id,typename')->select();
        $types = array();
        foreach ($type as $k => $v) {
            $types[$k]['unique'] = 'unique_' . $type[$k]['id'];
            $types[$k]['message'] = $type[$k]['typename'];
        }
        $hot = M('goods')->where('is_hot=1')->select();
        if ($hot) {
            foreach ($hot as $kk => $vv) {
                $hot[$kk]['img'] = explode(',', $vv['img']);
                $hot[$kk]['illustrate'] = json_decode($vv['illustrate']);
                $hot[$kk]['content'] = json_decode($vv['content']);
            }
            $num = 0;
            foreach ($hot as $hk => $hv) {
                if ($hk % 2 == 0 && $hk != 0) {
                    $num++;
                    $hots[$num][] = $hot[$hk];
                } else {
                    $hots[$num][] = $hot[$hk];
                }

            }
            $abc=count($hots);
            for($i=1;$i<=$abc;$i++){
                $count[$i][] = $abc;
                $count[$i][] = count($hots[$i]);
            }
            $score = M('goods')->order("score desc")->select();
            foreach ($score as $sk => $sv) {
                $score[$sk]['img'] = explode(',', $sv['img']);
            }
            $map['type'] = $types;
            $map['hot'] = $hots;
            $map['score'] = $score;
            $map['count'] = $count;
            echo json_encode($map);
        } else {
            $types['type'] = $types;
            echo json_encode($types);
        }

    }

    public function one()
    {
        $get = I('id');
        $re = M('goods')->where('id=' . $get)->find();
        $re['img'] = explode(',', $re['img']);
        $re['fa_img'] = json_decode( $re['fa_img']);
        foreach($re['fa_img'] as $k=>$v){
            $re['fa_img'][$k]= explode('++',$re['fa_img'][$k]);
        }
        $re['content'] = json_decode($re['content']);
        echo json_encode($re);
    }
    public function ones(){
        $id=I('id');//条id
        $last=I('last');//下一级
        $re=M('goods')->field('content,fa_img')->where('id='.$id)->find();
        $content=json_decode($re['content']);
        $map['content']['name']=$content->{'name'}[$last];
        $map['content']['price']=$content->{'price'}[$last];
        $map['content']['number']=$content->{'number'}[$last];
        $map['content']['unit']=$content->{'unit'}[$last];
        $map['fa_img']=explode('++',json_decode($re['fa_img'])[$last]);
       echo json_encode($map);
    }
    public function type(){
        $where=i('type');
        $type = M('type')->field('id,typename')->select();
        $types = array();
        foreach ($type as $k => $v) {
            $types[$k]['unique'] = 'unique_' . $type[$k]['id'];
            $types[$k]['message'] = $type[$k]['typename'];
        }
        $hot = M('goods')->where('type='.$where)->select();
        if ($hot) {
            foreach ($hot as $kk => $vv) {
                $hot[$kk]['img'] = explode(',', $vv['img']);
                $hot[$kk]['illustrate'] = json_decode($vv['illustrate']);
                $hot[$kk]['content'] = json_decode($vv['content']);
            }
            $num = 0;
            foreach ($hot as $hk => $hv) {
                if ($hk % 2 == 0 && $hk != 0) {
                    $num++;
                    $hots[$num][] = $hot[$hk];
                } else {
                    $hots[$num][] = $hot[$hk];
                }

            }
            $abc=count($hots);
            for($i=1;$i<=$abc;$i++){
                $count[$i][] = $abc;
                $count[$i][] = count($hots[$i]);
            }
            $score = M('goods')->order("score desc")->select();
            foreach ($score as $sk => $sv) {
                $score[$sk]['img'] = explode(',', $sv['img']);
            }
            $map['type'] = $types;
            $map['hot'] = $hots;
            $map['score'] = $score;
            $map['count'] = $count;
            echo json_encode($map);
        } else {
            $types['type'] = $types;
            echo json_encode($types);
        }
    }
}